package com.item.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.item.mapper.ShopMapper;
import com.item.pojo.Good;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopMapper shopMapper;

    //新增和修改
    @PostMapping
    public Object save(@RequestBody Good good){
        if (good.getId() == null) {
            return  shopMapper.insert(good);
        }
        return shopMapper.updateById(good);
    }


    @GetMapping("/pageList")
    public Page<Good> pageList(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String type){
        //操作数数据库对象
        QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("type",type);

        String sql="limit "+(pageNum-1)*pageSize+","+pageSize;

        Page<Good> videoPage =shopMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);

        videoPage.setTotal(shopMapper.selectCount(queryWrapper));

        videoPage.setRecords(shopMapper.selectList(queryWrapper.last(sql)));

        return videoPage;
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable Integer id){
        int delete = shopMapper.deleteById(id);
        return delete;
    }

    //批量删除
    @PostMapping("/del/batch")
    public int delBatch(@RequestBody List<Integer> ids){
        return shopMapper.deleteBatchIds(ids);
    }
}
